package com.itheima.d2_reflect_test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.Field;

/*
    对象框架
    目标：保存任意对象的字段和其数据到文件中去
 */
public class ObjectFrame {

    public static void saveObj(Object o) throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt",true));

        Class clazz = o.getClass();

        bw.write("========="+clazz.getSimpleName()+"===========");
        bw.newLine();

        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
//            System.out.println(declaredField.getName());
//            System.out.println(declaredField.get(o));
            String name = declaredField.getName();
            Object value = declaredField.get(o);
            bw.write(name +"="+value);
            bw.newLine();
            bw.flush();
        }

        bw.close();
    }
}
